
const { SIGNAL_DEFINE } = require("./signal");
const socket = require('../../base_net/socket');
const db = require("../../utils/db");

class RecordMgr {
    constructor() {
        this.actionList = [];
        this.signal = null;
        this.gameMgr = null;
        this.seatMgr = null;
    }

    setSignal(signal) {
        this.signal = signal;
        this.registerSignals();
    }

    registerSignals() {
        this.signal.register(SIGNAL_DEFINE.GAME_BEGIN, this.onGameBegin.bind(this));
        this.signal.register(SIGNAL_DEFINE.INIT_GAMBER_INFO, this.onInitGamberInfo.bind(this));
        this.signal.register(SIGNAL_DEFINE.DECIDE_BANKER, this.onDecideBanker.bind(this));
        this.signal.register(SIGNAL_DEFINE.SHOW_CARD, this.onShowCard.bind(this));
        this.signal.register(SIGNAL_DEFINE.BETTING, this.onBetting.bind(this));
        this.signal.register(SIGNAL_DEFINE.NEXT_GAME, this.onNextGame.bind(this));
        this.signal.register(SIGNAL_DEFINE.SAVE_GAME_RECORD, this.onSaveGameRecord.bind(this));
        this.signal.register(SIGNAL_DEFINE.SAVE_GAME_HISTORY, this.onSaveGameHistory.bind(this));
    }

    setGameMgr(gameMgr) {
        this.gameMgr = gameMgr;
    }
    
    setSeatMgr(seatMgr) {
        this.seatMgr = seatMgr;
    }

    onGameBegin(currentRound) {
        this.actionList = [];

        this.recordGameAction(socket.resp.game_begin);
        this.recordGameAction(socket.resp.game_num, currentRound);
    }

    onInitGamberInfo(gambers) {
        var allHolds = [];
        for (let i = 0; i < gambers.length; ++i) {
            if (gambers[i] == null) continue;
            var gamber = gambers[i];
            var isSit = this.seatMgr.isUserSitBySeatIndex(gamber.seatIndex);
            allHolds.push({userId:gamber.userId, holds:gamber.holds, sit:isSit});
        }
        this.recordGameAction(socket.resp.game_holds,allHolds);
    }

    onDecideBanker(userId, robSeatIndexs) {
        var userArr = [];
        for (let i = 0; i < robSeatIndexs.length; ++i) {
            var userId = this.seatMgr.getUserIdBySeatIndex(robSeatIndexs[i]);
            userArr.push(userId);
        }
        this.recordGameAction(socket.resp.dingzhuang_all,{time:this.gameMgr.decideBankerTime,button:userId,userArr:userArr});
    }

    onBetting(userId, value) {
        this.recordGameAction(socket.resp.xiazhu_notify,{userId:userId,value:value});
    }

    onShowCard() {
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (let i = 0; i < gamberIndexs.length; ++i) {
            var index = gamberIndexs[i];
            var gamber = this.seatMgr.getGamberBySeatIndex(index);
            this.recordGameAction(socket.resp.liangpai_notify, {userId: gamber.userId,holds: gamber.holds,sit:gamber.sit});
        }
    }

    onSaveGameRecord() {
        var dbusers = [];
        var dbresult = [];
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (let i = 0; i < gamberIndexs.length; ++i) {
            var index = gamberIndexs[i];
            var gamber = this.seatMgr.getGamberBySeatIndex(index);
            dbusers.push(gamber.userId);
            dbresult.push(gamber.score);
        }
        //记录打牌信息
        var roomId = this.gameMgr.getRoomId();
        var gameType = this.gameMgr.getGameType();
        var createTime = this.gameMgr.getCreateTime();
        var numOfGames = this.gameMgr.getCurrentRound();
        db.insert_game_records(roomId, gameType, createTime, numOfGames, dbusers, dbresult, this.actionList);
    }

    onSaveGameHistory() {
        var roomInfo = this.gameMgr.getRoomInfo();
        var history = {
            roomId:this.gameMgr.getRoomId(),
            type : this.gameMgr.getGameType(),
            owner : roomInfo.owner,
            index : this.gameMgr.getCurrentRound(),
            time: this.gameMgr.getCreateTime(),
            conf:roomInfo.conf,
            url : roomInfo.url,
            seats:[]
        };
    
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for(let i = 0; i < gamberIndexs.length; ++i){
            let index = gamberIndexs[i];
            let user = this.seatMgr.getUserByIndex(index);

            var hs = history.seats[index] = {};
            hs.userId = user.userId;
            hs.name = crypto.fromBase64(user.userName);
            hs.score = user.score;
        }
        db.store_history(history);
    }

    onNextGame() {
        this.recordGameAction(socket.resp.next_all,{time:this.gameMgr.settleTime});
    }

    recordGameAction(cmd, result){
        this.actionList.push({cmd:cmd, result:result});
    }

    getRecords() {
        return this.actionList;
    }
}

exports.RecordMgr = RecordMgr;